In this article, we will learn about file attributes in C#, how to set or get file attributes, and the FileAttributes
Enum.
FileAttributes Enum
The file Attributes is an Enum that contains attributes fields that can be set or get when dealing with files or directories.
Here are the lists of the fields of the FileAttributes Enum:
Field | Description |
---|---|
Archive | Windows processes set this attribute whenever a file is modified. |
Compressed | The file is compressed. |
Device | The file is reserved. |
Encrypted | The File or directory is encrypted. |
Hidden | The file is hidden. |
Device | The file is Reserved. |
Normal | The file is standard and has no special attributes |
NoScrubData | The file or directory is excluded from the data integrity scan. |
NotContentIndexed | The file will not be indexed by OS indexing service. |
Offline | The file is offline. |
ReadOnly | The file is read-only. |
ReparsePoint | The file contains user-defined data associated with a file or directory. |
SparseFile | The file is a sparse file. |
System | The file is a system file |
Temporary | The file is temporary. |
Get file attributes
private void frmMain_Load(object sender, EventArgs e) { StringBuilder stringBuilder = new StringBuilder(); string FileName = "MyFile.txt"; string FilePath = Application.StartupPath + "\" + FileName; FileAttributes attributes = File.GetAttributes(FilePath); MessageBox.Show(" The file attribute is: " + attributes.ToString()); }
Set file attributes
The File.SetAttributes
methods set the specified attributes of the file.
For example, if a file is hidden, you can show the file by removing its hidden attribute, and vise versa.
private void frmMain_Load(object sender, EventArgs e) { StringBuilder stringBuilder = new StringBuilder(); string FileName = "MyFile.txt"; string FilePath = Application.StartupPath + "\" + FileName; FileAttributes attributes = File.GetAttributes(FilePath); if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden) { // Remove the Hidden Attribute attributes = attributes & ~FileAttributes.Hidden; File.SetAttributes(FilePath, attributes); MessageBox.Show("The file " + FileName + " is no longer Hidden"); } else { // Add Hidden Attribute File.SetAttributes(FilePath, File.GetAttributes(FilePath) | FileAttributes.Hidden); MessageBox.Show("The file " + FileName + " is now Hidden"); } }
Get File attribute
To check the attribute’s value of the file you have to get the current file attributes first and use bitwise AND operator.
For example, if you need to check whether a particular file is read-only or not, use the following piece of code.
private void frmMain_Load(object sender, EventArgs e) { StringBuilder stringBuilder = new StringBuilder(); string FileName = "MyFile.txt"; string FilePath = Application.StartupPath + "\" + FileName; bool isReadOnly = ((File.GetAttributes(FilePath) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly); MessageBox.Show("File " + FileName + " is ReadyOnly? " + isReadOnly); }
Add file attributes to the existing ones
To set attributes to the existing ones get, the current file attributes first and then use bitwise OR operator with the specific attribute.
For example, add read-only and archive attribute to the file.
private void frmMain_Load(object sender, EventArgs e) { StringBuilder stringBuilder = new StringBuilder(); string FileName = "MyFile.txt"; string FilePath = Application.StartupPath + "\" + FileName; File.SetAttributes(FilePath, File.GetAttributes(FilePath) | (FileAttributes.Archive | FileAttributes.ReadOnly)); FileAttributes attributes = File.GetAttributes(FilePath); MessageBox.Show("File " + FileName + " attributes: " + attributes); }
Delete file attributes
To delete file attributes get the attributes of the file first and use & operator to remove an attribute. For example, the code below will delete the attribute, Hidden:
private void frmMain_Load(object sender, EventArgs e) { StringBuilder stringBuilder = new StringBuilder(); string FileName = "MyFile.txt"; string FilePath = Application.StartupPath + "\" + FileName; File.SetAttributes(FilePath, File.GetAttributes(FilePath) & ~FileAttributes.Hidden); FileAttributes attributes = File.GetAttributes(FilePath); MessageBox.Show("File " + FileName + " attributes: " + attributes); }
Related Articles
Comments